<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      @submit.native.prevent="() => handleQuery()"
    >
      <el-form-item :label="$t('employee_name')" prop="employeeName">
        <el-input
          v-model="queryParams.employeeName"
          :placeholder="$t('please_enter', { name: $t('employee_name') })"
          :show-word-limit="true"
          clearable
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >{{ $t("search") }}</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{
          $t("reset")
        }}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-radio-group v-model="queryParams.auditStatus" @change="getList">
        <el-radio-button :label="0">{{
          $t("pending_approval")
        }}</el-radio-button>
        <el-radio-button :label="1">{{ $t("approved") }}</el-radio-button>
      </el-radio-group>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="fileList"
      @selection-change="handleSelectionChange"
      border
      :max-height="tableHeight"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        :label="$t('employee_name')"
        align="center"
        prop="employeeName"
        :show-overflow-tooltip="true"
      />
      <!-- <el-table-column
        label="所属雇主"
        align="center"
        prop="employerName"
        :show-overflow-tooltip="true"
      /> -->
      <el-table-column
        :label="$t('employee_name')"
        align="center"
        prop="content"
        :show-overflow-tooltip="true"
      />
      <!-- <el-table-column
        label="修改人"
        align="center"
        prop="employeeName"
        :show-overflow-tooltip="true"
      /> -->
      <el-table-column
        :label="$t('submission_time')"
        align="center"
        prop="createTime"
        width="180"
      >
      </el-table-column>
      <el-table-column
        :label="$t('action')"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-if="scope.row.auditStatus == 0"
            v-hasPermi="['custom:employeeApprove:edit']"
            >{{ $t("approve") }}</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleHistoryView(scope.row)"
            v-hasPermi="['custom:employeeApprove:view']"
            >{{ $t("view") }}</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
      :page-sizes="[10,20,30,50,100, 200]"
    />

    <PofileUpdateInfo
      ref="pofileUpdateInfo"
      @confirm="submitForm"
    ></PofileUpdateInfo>
  </div>
</template>

<script>
import {
  listEmployeeLog,
  getEmployeeLog,
  auditEmployeeLog,
} from "@/api/custom/employeeApprove";
import PofileUpdateInfo from "./profileUpdateInfo";

export default {
  name: "File",
  components: {
    PofileUpdateInfo,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 雇员审批表格数据
      fileList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        employeeName: null,
        auditStatus: 0,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询雇员审批列表 */
    getList() {
      this.loading = true;
      listEmployeeLog(this.queryParams).then((response) => {
        this.fileList = response.data.records;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    handleHistoryView(row) {
      getEmployeeLog(row.id).then((res) => {
        this.$refs.pofileUpdateInfo.handleView(res.data);
      });
    },
    // 取消按钮
    cancel() {
      this.$prompt(
        this.$t("please_enter", { name: this.$t("reason_refusal") }),
        this.$t("reason_refusal"),
        {
          confirmButtonText: this.$t("confirm"),
          cancelButtonText: this.$t("cancel"),
          inputType: "textarea",
          inputPattern: /^.+$/,
          inputErrorMessage: this.$t("cannot_empty", {
            name: this.$t("reason_refusal"),
          }),
        }
      )
        .then(({ value }) => {
          auditEmployeeLog({
            ...this.form,
            auditStatus: 2,
            reason: value,
          }).then((response) => {
            this.$modal.msgSuccess(this.$t("operation_successful"));
            this.open = false;
            this.getList();
          });
        })
        .catch(() => {});
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        recordType: null,
        effectiveDate: null,
        content: null,
        turnoverTime: null,
        employeeName: null,
        employeeFilecol: null,
        modifier: null,
        beforeModification: null,
        afterModification: null,
        reasonModification: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      getEmployeeLog(row.id).then((res) => {
        this.$refs.pofileUpdateInfo.handleApproval(res.data);
      });
    },
    /** 提交按钮 */
    submitForm(data) {
      auditEmployeeLog({ ...data, auditStatus: 1 }).then((response) => {
        this.$modal.msgSuccess(this.$t("operation_successful"));
        this.getList();
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm(this.$t("delete_data"))
        .then(function () {
          return delFile(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess(this.$t("operation_successful"));
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/file/export",
        {
          ...this.queryParams,
        },
        `file_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
